package hello.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;

import hello.entity.Greeting;
import hello.entity.HelloMessage;
import hello.entity.OneToOne;

@Controller
public class GreetingController {
	
	@Autowired
	public SimpMessagingTemplate template;  

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        Thread.sleep(1000); // simulated delay
        return new Greeting("Hello, " + message.getName() + "!");
    }

    @MessageMapping("/single")
    @SendToUser(value="/topic/single",broadcast=false)
    public Greeting single(HelloMessage message) throws Exception {
        Thread.sleep(1000); // simulated delay
        return new Greeting("single, " + message.getName() + "!");
    }
    
    @MessageMapping("/onetoone")
    public void oneToOne(OneToOne one) throws Exception {
        Thread.sleep(1000); // simulated delay
        template.convertAndSendToUser(one.getTo(), "/topic/onetoone", one);
        //return one;
    }
}
